﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class ContextualGroupControl : UserControl, IControlRulable
    {
        private ContextualGroup innerContextualGroup;

        internal ContextualGroup InnerContextualGroup
        {
            get { return innerContextualGroup; }
            set { innerContextualGroup = value; }
        }

        public ContextualGroupControl()
        {
            InitializeComponent();
            this.ecColor.Values = Enum.GetValues(typeof(Enumerations.Color));
            this.innerContextualGroup = new ContextualGroup();
            this.FillControls();
        }

        public ContextualGroupControl(ContextualGroup cGroup)
            : this()
        {
            this.innerContextualGroup = cGroup;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.ecColor.SelectedValue = this.innerContextualGroup.Color.HasValue ? this.innerContextualGroup.Color.Value.ToString() : null;
                this.rcCommand.Value = this.innerContextualGroup.Command;
                this.strContextGroupId.Value = this.innerContextualGroup.ContextualGroupId;
                this.strId.Value = this.innerContextualGroup.Id;
                this.icSequence.Value = this.innerContextualGroup.Sequence;
                this.rcTitle.Value = this.innerContextualGroup.Title;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerContextualGroup.Color = (Enumerations.Color)Enum.Parse(typeof(Enumerations.Color), this.ecColor.SelectedValue);
                    this.innerContextualGroup.Command = this.rcCommand.Value;
                    this.innerContextualGroup.ContextualGroupId = this.strContextGroupId.Value;
                    this.innerContextualGroup.Id = this.strId.Value;
                    this.innerContextualGroup.Sequence = this.icSequence.Value;
                    this.innerContextualGroup.Title = this.rcTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ContextualGroupControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerContextualGroup.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
